package org.eiahe.hr.training.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.eiahe.hr.training.domain.bo.HrTrainingMaterialBo;
import org.eiahe.hr.training.domain.vo.HrTrainingMaterialVo;
import org.eiahe.hr.training.service.IHrTrainingMaterialService;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 培训课件Controller
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/training/material")
public class HrTrainingMaterialController extends BaseController {

    private final IHrTrainingMaterialService materialService;

    /**
     * 查询培训课件列表
     */
    @SaCheckPermission("hrTraining:material:list")
    @GetMapping("/list")
    public TableDataInfo<HrTrainingMaterialVo> list(HrTrainingMaterialBo bo, PageQuery pageQuery) {
        return materialService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取培训课件详细信息
     */
    @SaCheckPermission("hrTraining:material:query")
    @GetMapping("/{materialId}")
    public R<HrTrainingMaterialVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long materialId) {
        return R.ok(materialService.queryById(materialId));
    }

    /**
     * 新增培训课件
     */
    @SaCheckPermission("hrTraining:material:add")
    @Log(title = "培训课件", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HrTrainingMaterialBo bo) {
        return toAjax(materialService.insertByBo(bo));
    }

    /**
     * 修改培训课件
     */
    @SaCheckPermission("hrTraining:material:edit")
    @Log(title = "培训课件", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HrTrainingMaterialBo bo) {
        return toAjax(materialService.updateByBo(bo));
    }

    /**
     * 删除培训课件
     */
    @SaCheckPermission("hrTraining:material:remove")
    @Log(title = "培训课件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{materialIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] materialIds) {
        return toAjax(materialService.deleteByIds(Arrays.asList(materialIds)));
    }

    /**
     * 根据部门权限查询课件列表(员工端)
     */
    @GetMapping("/byDept/{deptId}")
    public R<List<HrTrainingMaterialVo>> getByDeptPermission(@PathVariable Long deptId) {
        List<HrTrainingMaterialVo> list = materialService.queryByDeptPermission(deptId);
        return R.ok(list);
    }

    /**
     * 增加查看次数
     */
    @PostMapping("/view/{materialId}")
    public R<Void> increaseView(@PathVariable Long materialId) {
        materialService.increaseViewCount(materialId);
        return R.ok();
    }

    /**
     * 增加下载次数
     */
    @PostMapping("/download/{materialId}")
    public R<Void> increaseDownload(@PathVariable Long materialId) {
        materialService.increaseDownloadCount(materialId);
        return R.ok();
    }

}
